package com.studentstreet.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.studentstreet.R;
import com.studentstreet.entities.CartEntity;
import com.studentstreet.entities.OrderEntity;
import com.studentstreet.tools.HtmlTools;

/**
 * @author likebamboo
 * 
 */
public class OrderTrackAdapter extends BaseExpandableListAdapter {

	private List<OrderEntity> orderList = new ArrayList<OrderEntity>();
	private Context context = null;

	public OrderTrackAdapter(Context context) {
		super();
		this.context = context;

	}

	public void addOrder(OrderEntity order) {
		orderList.add(order);
	}

	public void addOrderList(List<OrderEntity> list) {
		for (OrderEntity order : list) {
			orderList.add(order);
		}
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		return orderList.get(groupPosition).getCartList().get(childPosition);
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View view, ViewGroup parent) {
		if (view == null) {
			view = LayoutInflater.from(context).inflate(
					R.layout.order_list_item, null);
		}
		CartEntity cart = orderList.get(groupPosition).getCartList()
				.get(childPosition);
		// 大神经验：不可将这些TextView等控件声明为类成员，否则他们的监听器
		// 都会被最后一个Item的相应对象捕获
		TextView cartNameText = (TextView) view
				.findViewById(R.id.order_goods_name);
		TextView cartPriceText = (TextView) view
				.findViewById(R.id.order_goods_price);
		EditText cartCountEdit = (EditText) view
				.findViewById(R.id.order_goods_count);
		// 设置商品名
		cartNameText.setText(HtmlTools.getOrderGoods(context,
				cart.getCart_name(), cart.getCart_sellerName(),
				cart.getCart_state()));
		// 设置商品价格
		cartPriceText.setText(HtmlTools.getPrice(context, cart.getCart_price()
				+ ""));
		// 设置商品数量
		cartCountEdit.setText(cart.getCart_count() + "");
		return view;
	}

	// 某组的孩子数
	@Override
	public int getChildrenCount(int groupPosition) {
		return orderList.get(groupPosition).getCartList().size();
	}

	@Override
	public Object getGroup(int groupPosition) {
		return orderList.get(groupPosition);
	}

	// 组别数
	@Override
	public int getGroupCount() {
		return orderList.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = LayoutInflater.from(context).inflate(
					R.layout.order_track_item, null);
		}
		ImageView indicator = (ImageView) convertView
				.findViewById(R.id.track_indicator);
		if (isExpanded) {
			indicator.setBackgroundDrawable(convertView.getResources()
					.getDrawable(R.drawable.exlist_v));
		} else {
			indicator.setBackgroundDrawable(convertView.getResources()
					.getDrawable(R.drawable.exlist_h));
		}
		OrderEntity order = orderList.get(groupPosition);

		TextView no = (TextView) convertView.findViewById(R.id.track_order_no);
		TextView toUser = (TextView) convertView
				.findViewById(R.id.track_order_toUser);
		TextView toAddr = (TextView) convertView
				.findViewById(R.id.track_order_toAddr);
		TextView total = (TextView) convertView
				.findViewById(R.id.track_order_total);

		// 设置订单信息
		no.setText(HtmlTools.getOrderNo(context, order.getNo(),
				order.getState()));
		// 设置收餐人信息
		toUser.setText(HtmlTools.getOrderUser(context, order.getToUser()));
		// 设置送餐信息
		toAddr.setText(HtmlTools.getOrderAddr(context, order.getToAddr()));
		double price = 0d;
		int count = 0;
		for (CartEntity cart : orderList.get(groupPosition).getCartList()) {
			price += cart.getCart_price() * cart.getCart_count();
			count += cart.getCart_count();
		}
		total.setText(HtmlTools.getOrderTotal(context, price + "", count + ""));

		return convertView;
	}

	@Override
	public boolean hasStableIds() {
		return false;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return false;
	}

}
